#include <string.h>
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\def.h"

void _Zdma0XdreqHandshake(void);

#define BUS8		(0)
#define BUS16		(1)
#define BUS32		(2)
#define ENWAIT		(1)
#define SRAMBE03	(1)

//PG3 = DMAMODE0
//PG4 = DMAMODE1
//PE4 = nDMASTART
//PE5 = reserved

#define SET_XDREQ16()	rPDATG=(rPDATG&~(3<<3))|(0x1<<3)
#define SET_XDREQ1()	rPDATG=(rPDATG&~(3<<3))|(0x2<<3)
#define START_XDREQ()	{rPDATE=(rPDATE&~(1<<4))|(0x0<<4);\
			 rPDATE=(rPDATE&~(1<<4))|(0x1<<4);}



//DMAMODE[1:0]:	01b= one time request   
//		10b= 16 time requests

#define B1_Tacs		(0x0)	//0clk
#define B1_Tcos		(0x0)	//0clk
#define B1_Tacc		(0x1)	//2clk
#define B1_Tcoh		(0x0)	//0clk
#define B1_Tah		(0x0)	//0clk
#define B1_Tacp		(0x0)	//2clk
#define B1_PMC		(0x0)	//no page mode



volatile int isZdma0Done;




void __irq IsrZdma0Done(void)
{
    rI_ISPC=BIT_ZDMA0;
    isZdma0Done=1;
}




void Test_ZDma0Xdreq(void)
{
       
    rBWSCON=rBWSCON&(~0xf0)|(BUS16<<4);
    rBANKCON1=((B1_Tacs<<13)+(B1_Tcos<<11)+(B1_Tacc<<8)+(B1_Tcoh<<6)+(B1_Tah<<4)+(B1_Tacp<<2)+(B1_PMC));

    rINTMSK|=BIT_GLOBAL;
    pISR_ZDMA0=(U32)IsrZdma0Done;
    rINTMSK=~(BIT_GLOBAL|BIT_ZDMA0);

    rPCONF=rPCONF&(~(0xf<<6))|(0xf<<6);//PF3=nXDACK0,PF4=nXDREQ0

    rPCONE=rPCONE&(~(3))|(3);	    //PE0=CLKOUT

    rPDATG=rPDATG&(3<<3);
    rPCONG=rPCONG&~(0xf<<6)|(5<<6); //PG3,4=OUTPUT
    rPDATE=rPDATE&~(1<<4)|(1<<4);   //PE4=H
    rPCONE=rPCONE&~(3<<8)|(1<<8);   //PE4=OUTPUT
    
    _Zdma0XdreqHandshake();


}
void _Zdma0XdreqHandshake(void)
{
    int i;
    static U16 bufDst[16];
    static U16 bufSrc[16];

    Uart_Printf("[ZDMA0,HandShake,unit,16bit Test]\n");

    for(i=0;i<16;i++){bufSrc[i]=i;bufDst[i]=0;}
    
    isZdma0Done=0;
    rNCACHBE0=( ( (((unsigned)bufDst+16*4)>>12) +1 )<<16 )|((unsigned)bufDst>>12);

    rINTMSK=~(BIT_GLOBAL|BIT_ZDMA0);
    rZDISRC0=(U32)bufSrc|(1<<30)|(1<<28); // half-word,inc
    rZDIDES0=(U32)bufDst|((U32)2<<30)|(1<<28);  // normal,inc
    rZDICNT0=32|(0x0<<30)|(0x0<<28)|(0x1<<26)|(0x2<<24)|(0x3<<22)|(0x1<<21)|(0x1<<20);
    	//nXDREQ0,handshake,unit,terminal_int,auto-reload,enable DMA,
    rZDCON0=0x0; // nXDREQ0 enable,CMD=no_command.

    SET_XDREQ16();
    START_XDREQ();
    
    while(isZdma0Done==0);//Uart_Printf("ZDCCNT0=%08x        \r",rZDCCNT0);

    for(i=0;i<16;i++)Uart_Printf("%x,",*((U16 *)bufDst+i));
    Uart_Printf("\n");

    Cache_Flush();
    rNCACHBE0=0;

}

